package edu.thu.studio.ormdesigner.editor.action;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.ui.IWorkbenchPart;

import edu.thu.studio.ormdesigner.editor.ORMGraphicalEditor;
import edu.thu.studio.ormdesigner.model.diagram.Diagram;

public abstract class AbstractSelectionAction extends SelectionAction {
    
    private ORMGraphicalEditor editor;
    
    public AbstractSelectionAction(IWorkbenchPart part) {
        super(part);
        this.editor = (ORMGraphicalEditor) part;
    }
    
    protected GraphicalViewer getGraphicalViewer() {
        return this.editor.getGraphicalViewer();
    }
    
    protected Diagram getCurrentDiagram() {
        EditPart editPart = this.editor.getGraphicalViewer().getContents();
        Diagram diagram = (Diagram) editPart.getModel();
        return diagram;
    }
    
    @SuppressWarnings("unchecked")
    protected EditPart getEditPart() {
        List<EditPart> list = new ArrayList<EditPart>(this.getSelectedObjects());
        if (list.size() == 1) {
            return list.get(0);
        }
        return null;
    }
}
